﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using PPAP.BO;
using PPAP.Model;

namespace PPAP.Web.Part
{
    public partial class SamplingReport : PageBase
    {
        PPARBO pbo = new PPARBO();
        SamplecardBO sbo = new SamplecardBO();
        T_FileBO manager = new T_FileBO();
        LogBO lbo = new LogBO();
        public string rid { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["rid"] != null)
                {
                    rid = Request.QueryString["rid"].ToString();
                    DataSet ds = pbo.GetReportByPPARid(Request.QueryString["rid"].ToString());
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        DataRow dr = ds.Tables[0].Rows[0];
                        DataSet dsfile = manager.GetFileList(dr["DEVIATIONSHEET"].ToString());
                        //lblDecision.Text = dr["DECISIONTEXT"].ToString();
                        // lblExplanations.Text = dr["explanations"].ToString()==""?dr["remarks"].ToString():dr["explanations"].ToString();
                        lblQe.Text = dr["quanlityengineer"].ToString();
                        lblQM.Text = dr["quanlitymanager"].ToString();
                        lblQVP.Text = dr["quanlityvp"].ToString();
                        lblSamplingReportNo.Text = dr["samplingreportno"].ToString();
                        lblSupplier.Text = dr["suppliername"].ToString();

                        int height = 80;
                        if (dr["explanations"].ToString().Trim() == "")  //如果数据库中未保存，则自动生成
                        {
                            DataSet ds2 = sbo.GetList(" s.pparid=" + Request.QueryString["rid"].ToString() + "  and s.status<>9 ");
                            foreach (DataRow drr in ds2.Tables[0].Rows)
                            {
                                if (drr["RESULT"].ToString() == "Y" || drr["RESULT"].ToString() == "R")
                                {
                                    height += 44;
                                    lblExplanations.InnerHtml += drr["testitemname"].ToString() + "\n" + "Reason for deviation is " + drr["reason"].ToString() + "\n\n";
                                }
                            }
                            lblExplanations2.Text = lblExplanations.InnerHtml;
                            //height = height > 42 ? height - 23 : height;


                        }
                        else
                        {
                            DataSet ds2 = sbo.GetList(" s.pparid=" + Request.QueryString["rid"].ToString() + "  and s.status<>9 ");
                            foreach (DataRow drr in ds2.Tables[0].Rows)
                            {
                                if (drr["RESULT"].ToString() == "Y" || drr["RESULT"].ToString() == "R")
                                {
                                    height += 44;
                                    //lblExplanations.Text += drr["testitemname"].ToString() + "\n" + "Reason for deviation is " + drr["reason"].ToString() + "\n\n";
                                }
                            }
                            lblExplanations.InnerHtml = dr["explanations"].ToString();
                        }
                        lblExplanations2.Text = lblExplanations.InnerHtml;
                        lblExplanations.InnerHtml = lblExplanations.InnerHtml.Replace("\r\n", "<br>").Replace("\n", "<br>");
                        //lblExplanations.Height = height<80?80:height;
                        height = 20;
                        if (dr["DECISIONTEXT"].ToString().Trim() == "")//如果数据库中未保存，则自动生成
                        {
                            if (dr["elevel"].ToString() == "E")
                            {
                                lblDecision.InnerHtml = "No series delivery release can be issued.";
                            }
                            else
                            {

                                if (dr["finalresult"].ToString() == "G")
                                {
                                    lblDecision.InnerHtml = "The series delivery release has been issued";
                                }
                                if (dr["finalresult"].ToString() == "Y")
                                {
                                    lblDecision.InnerHtml = "Only a conditional series delivery release has been issued";
                                }
                                if (dr["finalresult"].ToString() == "R")
                                {
                                    lblDecision.InnerHtml = "The series delivery release has not been issued";
                                }
                                height += 44;
                                if (dr["partrelatedresult"].ToString() == "G")
                                {
                                    lblDecision.InnerHtml += "\n" + "Product:deviations from our specifications have not been detected ";
                                }
                                if (dr["partrelatedresult"].ToString() == "Y")
                                {
                                    lblDecision.InnerHtml += "\n" + "Product:deviations from our specifications have been detected ";
                                }
                                if (dr["partrelatedresult"].ToString() == "R")
                                {
                                    lblDecision.InnerHtml += "\n" + "Product:customer relevant deviations from our specifications have been detected";
                                }
                                height += 44;

                                if (dr["processrelatedresult"].ToString() == "G")
                                {
                                    lblDecision.InnerHtml += "\n" + "Process:approval has been given";
                                }
                                if (dr["processrelatedresult"].ToString() == "Y")
                                {
                                    lblDecision.InnerHtml += "\n" + "Process:deviations have been detected ";
                                }
                                if (dr["processrelatedresult"].ToString() == "R")
                                {
                                    lblDecision.InnerHtml += "\n" + "Process:severe deviations have been detected";
                                }
                            }
                            if (dsfile.Tables[0].Rows.Count > 0 && dr["resubmissiondate"].ToString() != "" && DateTime.Parse(dr["resubmissiondate"].ToString()) > DateTime.Parse("1900-02-01"))
                            {
                                height += 44;
                                lblDecision.InnerHtml += "\nWe expect an optimized product and/or process as well as a new PPA-Report latest till the re-submission date " + DateTime.Parse(dr["resubmissiondate"].ToString()).ToString("yyyy-MM-dd");
                            }
                            //height = height > 42 ? height - 23 : height;
                            lblDecision2.Text = lblDecision.InnerHtml;
                        }
                        else
                        {
                            if (dr["elevel"].ToString() == "E")
                            {
                               // lblDecision.Text = "No series delivery release can be issued.";
                            }
                            else
                            {
                                height += 44;
                                height += 44;

                            }
                            if (dsfile.Tables[0].Rows.Count > 0 && dr["resubmissiondate"].ToString() != "" && DateTime.Parse(dr["resubmissiondate"].ToString()) > DateTime.Parse("1900-02-01"))
                            {
                                height += 44;
                                //lblDecision.Text += "\nWe expect an optimized product and/or process as well as a new PPA-Report latest till the re-submission date " + DateTime.Parse(dr["resubmissiondate"].ToString()).ToString("yyyy-MM-dd");
                            }
                            lblDecision.InnerHtml = dr["DECISIONTEXT"].ToString();
                        }
                        lblDecision2.Text = lblDecision.InnerHtml;
                        lblDecision.InnerHtml = lblDecision.InnerHtml.Replace("\r\n", "<br>").Replace("\n", "<br>");
                        //lblDecision.Height = height < 80 ? 80 : height ;
                        //lblDecision.Disabled = true;
                        //lblExplanations.Disabled = true;
                        if (dr["status"].ToString() == "6"&&(dr["FINALSTATUS"].ToString()=="0"||dr["FINALSTATUS"].ToString()=="4"))
                        {
                            //lblDecision.Disabled = false;
                           // lblExplanations.Disabled = false;
                            lblDecision.Visible = false;
                            lblExplanations.Visible = false;
                            lblDecision2.Visible = true;
                            lblExplanations2.Visible = true;
                            btnSubmit.Visible = true;
                        }
                        //if (dr["status"].ToString() == "7")
                        //{
                        lblResult.Text = dr["finalresult"].ToString() == "R" ? "Rejected" : dr["finalresult"].ToString() == "Y" ? "Conditionally approved" :dr["finalresult"].ToString()=="G"? "Approved":"";
                        //}
                    }

                    if (Request.QueryString["his"] != null && Request.QueryString["his"].ToString() == "1") //从histroy页面跳转过来
                    {
                        //lblDecision.Disabled = true;
                        //lblExplanations.Disabled = true;
                        btnSubmit.Visible = false;
                    }

                    
                }
            }
            if (Request.QueryString["rid"] != null)
            {
                rid = Request.QueryString["rid"].ToString();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["rid"] != null)
            {
                PPAR p = pbo.GetModel(int.Parse(Request.QueryString["rid"].ToString()));
                if (p != null)
                {
                    if (p.RESUBMISSIONDATE == null)
                    {
                        p.RESUBMISSIONDATE = DateTime.Parse("1900-01-01");
                        
                    }
                    if (p.DEVIATIONEXPIREDATE == null)
                    {
                        p.DEVIATIONEXPIREDATE = DateTime.Parse("1900-01-01");
                    }
                    if (p.APPROVALSTATUSBYQM == null)
                        p.APPROVALSTATUSBYQM = 0;
                    if (p.APPROVALSTATUSBYVP == null)
                        p.APPROVALSTATUSBYVP = 0;
                    p.EXPLANATIONS = CheckWord(lblExplanations2.Text);
                    p.DECISIONTEXT = CheckWord(lblDecision2.Text);
                    pbo.Update(p);
                   // lbo.WriteLog(CurrentUser.Id, "Save Sampling Report", Request.QueryString["rid"].ToString());
                    AlertMessage("Submit succeed");
                }
            }
        }

        protected void lblDecision2_TextChanged(object sender, EventArgs e)
        {
            if (Request.QueryString["rid"] != null)
            {
                PPAR p = pbo.GetModel(int.Parse(Request.QueryString["rid"].ToString()));
                if (p != null)
                {
                    if (p.RESUBMISSIONDATE == null)
                    {
                        p.RESUBMISSIONDATE = DateTime.Parse("1900-01-01");

                    }
                    if (p.DEVIATIONEXPIREDATE == null)
                    {
                        p.DEVIATIONEXPIREDATE = DateTime.Parse("1900-01-01");
                    }
                    if (p.APPROVALSTATUSBYQM == null)
                        p.APPROVALSTATUSBYQM = 0;
                    if (p.APPROVALSTATUSBYVP == null)
                        p.APPROVALSTATUSBYVP = 0;
                    p.EXPLANATIONS = CheckWord(lblExplanations2.Text);
                    p.DECISIONTEXT = CheckWord(lblDecision2.Text);
                    pbo.Update(p);
                    //lbo.WriteLog(CurrentUser.Id, "Auto Save Sampling Report", Request.QueryString["rid"].ToString());
                }
            }
        }
    }
}